import {Injectable} from "@angular/core";
import {HttpHelper} from "./HttpHelper";
import {Manufacture, ProductionBill, ManufactureItem} from "./domains";
@Injectable()
export class ProductionBillService{
    constructor(private helper:HttpHelper){

    }
    bills(page?,size?):Promise<ProductionBill[]>{
        let url = "production-bill";
        if (page&&size){
            url = url + `/?page=${page}&size=${size}`;
        }
        return this.helper.getJson<ProductionBill[]>(url);
    }
    authorBills(page?,size?): Promise<ProductionBill[]> {
        let url = "production-bill/author"
        if (page&&size) {
            url = url + `/?page=${page}&size=${size}`;
        } else {
            url = url + `/?page=0`;
        }
        return this.helper.getJson<ProductionBill[]>(url);
    }
    postBill(manufactureId:number,qty:number,liable:number,expectTime:string,author:number):Promise<ProductionBill>{
        let bill:ProductionBill=new ProductionBill();
        let item:ManufactureItem=new ManufactureItem();
        item.manufacture= new Manufacture();
        item.manufacture.id = manufactureId;
        item.quantity=qty;
        bill.item=item;
        bill.expectEndTime=expectTime.replace(' ','T');
        if (liable !=null ) {
            bill.liable = {id:liable};
        }
        if (author != null) {
            bill.author = {id:author};
        }
        return this.helper.postToJson("production-bill",bill);
    }

    bill(id:number):Promise<ProductionBill>{
        return this.helper.getJson<ProductionBill>("production-bill/"+id);
    }

    startBill(billID:number): Promise<ProductionBill> {
        let url = `production-bill/start/bill=${billID}`;
        return this.helper.getJson<ProductionBill>(url);
    }

    endBill(billID:number): Promise<ProductionBill> {
        let url = `production-bill/end/bill=${billID}`;
        return this.helper.getJson<ProductionBill>(url);
    }

    receiveAddLeader(billId): Promise<ProductionBill> {
        let url = `production-bill/receive/?bill=${billId}`;
        return this.helper.putToJson<ProductionBill>(url,null);
    }
}